<template>
    <div class="l-rblock l-form-viewer" style="padding:8px;">
        <el-form
            :model="formData"
            :rules="rules"
            size="mini"
            labelPosition="right"
            labelWidth="110px"
            ref="form"
            >
            <div class="l-auto-window only-tabs">
                <el-tabs v-model="activeName" type="card">
                    <el-tab-pane class="l-tabs-container" :label="$t('主表信息')" name="tab0">
                        <el-row :gutter="0">
                            <div class="l-rblock">
                                <el-col v-if="lr_formLookAuth('ew_matecarbasic_f_pcdh')" :span="24" >
                                    <el-form-item 
                                        label="配车单号" 
                                        prop="ew_matecarbasic_f_pcdh"
                                        > 
                                        <l-code size="mini" code="demo_ew_pcdh" v-model="formData.ew_matecarbasic_f_pcdh" />
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('ew_matecarbasic_f_djrq')" :span="24" >
                                    <el-form-item 
                                        label="单据日期" 
                                        prop="ew_matecarbasic_f_djrq"
                                        > 
                                        <l-date  :disabled="!lr_formEditAuth('ew_matecarbasic_f_djrq')" format="yyyy-MM-dd" dateType="date" :clearable="true" v-model="formData.ew_matecarbasic_f_djrq" :placeholder="$t('请选择')" />
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('ew_matecarbasic_f_yscl')" :span="24" >
                                    <el-form-item 
                                        label="运输车辆" 
                                        prop="ew_matecarbasic_f_yscl"
                                        > 
                                        <el-input :disabled="!lr_formEditAuth('ew_matecarbasic_f_yscl')" v-model="formData.ew_matecarbasic_f_yscl" :placeholder="$t('请输入')" >
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('ew_matecarbasic_f_jsybh')" :span="24" >
                                    <el-form-item 
                                        label="驾驶员" 
                                        prop="ew_matecarbasic_f_jsybh"
                                        > 
                                        <l-select :disabled="!lr_formEditAuth('ew_matecarbasic_f_jsybh')"  :options="ew_matecarbasic_f_jsybhOptions" v-model="formData.ew_matecarbasic_f_jsybh" />
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('ew_matecarbasic_f_zdr')" :span="24" >
                                    <el-form-item 
                                        label="制单人" 
                                        prop="ew_matecarbasic_f_zdr"
                                        > 
                                        <l-createuser v-model="formData.ew_matecarbasic_f_zdr" />
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('ew_matecarbasic_f_zdsj')" :span="24" >
                                    <el-form-item 
                                        label="制单时间" 
                                        prop="ew_matecarbasic_f_zdsj"
                                        > 
                                        <l-createtime v-model="formData.ew_matecarbasic_f_zdsj" />
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('ew_matecarbasic_f_bz')" :span="24" >
                                    <el-form-item 
                                        label="备注" 
                                        prop="ew_matecarbasic_f_bz"
                                        > 
                                        <el-input type="textarea"  :disabled="!lr_formEditAuth('ew_matecarbasic_f_bz')" :rows="4" v-model="formData.ew_matecarbasic_f_bz" :placeholder="$t('请输入')" />
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('1655101499382_11584')" :span="24" >
                                    <div class="l-title" >{{$t('配车单详情')}}</div>
                                    <l-edit-table
                                        ref="ew_matecardetail_table"
                                        addBtnText="新增一行"
                                        :isAddBtn="true"
                                        :isRemoveBtn="true"
                                        :isShowNum="true"
                                        :dataSource="ew_matecardetail_data"
                                        :columns="lr_formTableAuth('ew_matecardetail',ew_matecardetail_columns)"
                                        @addRow="handleEw_matecardetailAddRow"
                                        @deleteRow="handleEw_matecardetailDeleteRow"
                                        >
                                        <template v-slot:f_ddbh="scope" >
                                            <l-code size="mini" code="demo_ew_khdd" v-model="scope.row.f_ddbh" />
                                        </template>
                                        <template v-slot:f_khbh="scope" >
                                            <l-select :disabled="!lr_formTableEditAuth('ew_matecardetail','f_khbh')"  size="mini" :options="ew_matecardetail_f_khbhOptions" v-model="scope.row.f_khbh" />
                                        </template>
                                        <template v-slot:f_cpbh="scope" >
                                            <l-select 
                                            :disabled="!lr_formTableEditAuth('ew_matecardetail','f_cpbh')"  
                                            size="mini" 
                                            :options="ew_matecardetail_f_cpbhOptions" 
                                            labelKey="f_ItemName"
                                            valueKey="f_ItemName"
                                            v-model="scope.row.f_cpbh" 
                                            @change="handleChange(scope)" />
                                        </template>
                                        <template v-slot:f_cpgg="scope" >
                                            <el-input size="mini"  :disabled="!lr_formTableEditAuth('ew_matecardetail','f_cpgg')" v-model="scope.row.f_cpgg" :placeholder="$t('请输入')" >
                                            </el-input>
                                        </template>
                                        <template v-slot:f_bcfhsl="scope" >
                                            <el-input size="mini"  :disabled="!lr_formTableEditAuth('ew_matecardetail','f_bcfhsl')" v-model="scope.row.f_bcfhsl" :placeholder="$t('请输入')" >
                                            </el-input>
                                        </template>
                                    </l-edit-table>
                                </el-col>
                            </div>
                        </el-row>
                    </el-tab-pane>

                </el-tabs>
            </div>
        </el-form>
    </div>
</template>
<script>
const api = window.$api.demo.matecarbasic

export default {
    data(){
        return {
            moduleCode: 'demo_matecarbasic',
            activeName: 'tab0',
            formData: {
                ew_matecarbasic_f_pcdh:"", // 配车单号
                ew_matecarbasic_f_djrq:"", // 单据日期
                ew_matecarbasic_f_yscl:"", // 运输车辆
                ew_matecarbasic_f_jsybh:"", // 驾驶员
                ew_matecarbasic_f_zdr:"", // 制单人
                ew_matecarbasic_f_zdsj:"", // 制单时间
                ew_matecarbasic_f_bz:"", // 备注

            },
            rules: {
                ew_matecarbasic_f_djrq:[
                    { required: true, message: '请选择单据日期' },
                ],
                ew_matecarbasic_f_yscl:[
                    { required: true, message: '请输入运输车辆' },
                ],
                ew_matecarbasic_f_jsybh:[
                    { required: true, message: '请选择驾驶员' },
                ],

            },
            ew_matecardetail_columns:[
                {id:'ew_matecardetail_F_ddbh',prop:'f_ddbh',label:'订单编号'},
                {id:'ew_matecardetail_F_khbh',prop:'f_khbh',label:'客户',required:true},
                {id:'ew_matecardetail_F_cpbh',prop:'f_cpbh',label:'产品',required:true},
                {id:'ew_matecardetail_F_cpgg',prop:'f_cpgg',label:'产品规格',required:true},
                {id:'ew_matecardetail_F_bcfhsl',prop:'f_bcfhsl',label:'本次发货数量',required:true,patterns:[{"msg":"请输入数字","reg":"/^-?[0-9]+$/"}]},
            ],
            ew_matecardetail_data:[],

        };
    },
    computed: {
        ew_matecarbasic_f_jsybhOptions(){
            this.lr_loadDataItem('jsy')
            return this.lr_dataItemOptions(this.lr_dataItem['jsy'])
        },
        ew_matecardetail_f_khbhOptions(){
            this.lr_loadDataItem('kh')
            return this.lr_dataItemOptions(this.lr_dataItem['kh'])
        },
        ew_matecardetail_f_cpbhOptions(){
            this.lr_loadDataItem('cp')
            return this.lr_dataItemOptions(this.lr_dataItem['cp'])
        },

    },
    methods: {
        handleEw_matecardetailAddRow(){
            let point = {}
            point.f_ddbh = ""
            point.f_khbh = ""
            point.f_cpbh = ""
            point.f_cpgg = ""
            point.f_bcfhsl = ""
            this.ew_matecardetail_data.push(point)
        },
        handleEw_matecardetailDeleteRow(event){
            this.ew_matecardetail_data.splice(event.index,1)
        },

        
        // 重置表单
        resetForm() {
            this.$formClear(this.$refs.form)
            this.activeName = 'tab0'
            this.ew_matecardetail_data = []

        },
        // 校验表单
        async validateForm() {
            if (!await this.$formValidateWraper(this.$refs.form)) {
                return false  
            }
            if(!this.$refs.ew_matecardetail_table.validate())
            {
                return false
            }

            return true
        },
        // 设置表单数据
        setForm(data) {
            this.$setFormData(this.formData,data.ewMatecarbasicEntity,'ew_matecarbasic')
            this.ew_matecardetail_data = data.ewMatecardetailList

        },
        // 获取表单数据
        getForm() {
            const formData = {}
            formData.ewMatecarbasicEntity = this.$getFormData(this.formData,'ew_matecarbasic')
            formData.ewMatecardetailList = this.ew_matecardetail_data
            return formData

        },

        async loadFormData({ keyValue/*,params,node*/ }) {//  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
            const data = await this.$awaitWraper(api.get(keyValue))
            if (data) {
                this.setForm(data)
                return true
            }
            else {
                return false
            }
        },

        async saveForm({ keyValue, isEdit/*,code,node*/ }) {// isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
            const postData = this.getForm()
            if (isEdit) {// 编辑
                return await this.$awaitWraper(api.update(keyValue, postData))
            }
            else {// 新增
                postData.ewMatecarbasicEntity.f_Id = keyValue

                return await this.$awaitWraper(api.add(postData))
            }
        },
        handleChange(scope){
            var data=this.ew_matecardetail_f_cpbhOptions.find(t=>t.f_ItemName == this.ew_matecardetail_data[scope.$index].f_cpbh)
            if(data){
                this.ew_matecardetail_data[scope.$index].f_cpgg = data.f_ItemValue;
            }
        },
    }
}
</script>